<?php
namespace app\commentConf\controller;
use app\common\Base;
use app\common\Compent;
use app\common\Emodel;
use think\Db;

class ParamConf extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$page = input('page');
		$rows = input('rows');
		$where["DICT_TYPE_ID"] = input("dictTypeId");
		$result = Db::table("T_SYS_DICT_DATA")
				->where($where)
				->page($page, $rows)
				->order("CREATE_TIME")
				->select();
		$count = Db::table("T_SYS_DICT_DATA")
				->where($where)
				->count();
		$grid['rows'] = $result;
		$grid['total'] = $count;
		return $grid;
	}

	public function dictType() {
		return Db::table("T_SYS_DICT_TYPE")
			   ->where("RANGE = 'SYS'")
			   ->select();
	}

	public function dictTypeById() {
		$where["ID"] = input("id");
		return Db::table("T_SYS_DICT_TYPE")
			   ->where($where)
			   ->find();
	}

	public function dictDataById() {
		$where["ID"] = input("id");
		return Db::table("T_SYS_DICT_DATA")
			   ->where($where)
			   ->find();
	}

	public function dictCodeExists() {
		$where["DICT_CODE"] = input("dictCode");
		return Db::table("T_SYS_DICT_TYPE")
			   ->where($where)
			   ->count();
	}

	public function addUpdateType() {
		$all = request()->param();
		try {
			if(array_key_exists("ID", $all) && $all["ID"]) {
				update('T_SYS_DICT_TYPE', $all);
			} else {
				unset($all["ID"]);
				insert('T_SYS_DICT_TYPE', $all);
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function addUpdateData() {
		$all = request()->param();
		try {
			if(array_key_exists("ID", $all) && $all["ID"]) {
				update('T_SYS_DICT_DATA', $all);
			} else {
				unset($all["ID"]);
				$count = Db::table("T_SYS_DICT_DATA")
						 ->where("DICT_TYPE_ID = ".$all["DICT_TYPE_ID"])
						 ->count();
				$all["DICT_DATA_KEY"] = $count + 1;
				insert('T_SYS_DICT_DATA', $all);
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

}
